<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程章节管理 
 */
class Chapter extends Backend
{
    /**
     * 是否是关联查询
     */
    protected $relationSearch = true;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Subject.Chapter');
    }
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $subid = $this->request->param('subid', 0, 'trim');
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error("参数不能为空");
            }
            $params['subid'] = $subid;
            $result = $this->model->validate('common/Subject/Chapter')->save($params);
            if (!$result) {
                $this->error($this->model->getError());
            }
            $this->success();
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('当前课程不存在'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $params['id'] = $ids;
            if (empty($params)) {
                $this->error("参数不能为空");
            }
            $result = $this->model->isUpdate()->save($params);
            if (!$result) {
                // 判断视频是否有上传
                if (!empty($params['url']) && $params['url'] != $row['url']) {
                    is_file(ltrim($params['url'], '/')) && unlink(ltrim($params['url'], '/'));
                }
                $this->error();
            }
            // 判断视频是否有上传
            if (!empty($params['url']) && $params['url'] != $row['url']) {
                is_file(ltrim($row['url'], '/')) && unlink(ltrim($row['url'], '/'));
            }
            $this->success();
        }

        $this->assign('row', $row);
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = null)
    {
        $categoryList = $this->model->all($ids);
        if (empty($categoryList)) {
            $this->error('当前章节不存在');
        }
        $result = $this->model->destroy($ids);
        if (!$result) {
            $this->error($this->model->getError());
        }
        foreach ($categoryList as $item) {
            is_file(ltrim($item['url'], '/')) && unlink(ltrim($item['url'], '/'));
        }
       
        $this->success();
    }
}
